<template>
  <div class="layer-panel">
    <div class="header">
      <h3>图层</h3>
      <div class="actions">
        <el-button type="text" size="small" @click="handleAddLayer">
          <i class="el-icon-plus"></i>
        </el-button>
        <el-button type="text" size="small" @click="handleDeleteLayer">
          <i class="el-icon-delete"></i>
        </el-button>
      </div>
    </div>
    <div class="content">
      <div class="layer-list">
        <div 
          v-for="(layer, index) in layers" 
          :key="index"
          class="layer-item"
          :class="{ active: activeLayer === layer.id }"
          @click="handleLayerClick(layer)"
        >
          <div class="layer-info">
            <i :class="layer.icon"></i>
            <span>{{ layer.name }}</span>
          </div>
          <div class="layer-actions">
            <el-switch
              v-model="layer.visible"
              size="small"
              @change="handleVisibilityChange(layer)"
            />
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'LayerPanel',
  data() {
    return {
      activeLayer: '',
      layers: [
        { id: '1', name: '背景', icon: 'el-icon-picture', visible: true },
        { id: '2', name: '文本', icon: 'el-icon-edit', visible: true },
        { id: '3', name: '形状', icon: 'el-icon-copy-document', visible: true }
      ]
    };
  },
  methods: {
    handleLayerClick(layer) {
      this.activeLayer = layer.id;
      this.$emit('layer-selected', layer);
    },
    handleVisibilityChange(layer) {
      this.$emit('layer-visibility-change', layer);
    },
    handleAddLayer() {
      const newLayer = {
        id: Date.now().toString(),
        name: '新图层',
        icon: 'el-icon-document',
        visible: true
      };
      this.layers.push(newLayer);
      this.$emit('layer-added', newLayer);
    },
    handleDeleteLayer() {
      if (this.activeLayer) {
        const index = this.layers.findIndex(layer => layer.id === this.activeLayer);
        if (index > -1) {
          const deletedLayer = this.layers.splice(index, 1)[0];
          this.$emit('layer-deleted', deletedLayer);
          this.activeLayer = '';
        }
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.layer-panel {
  padding: 20px;
  
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;

    h3 {
      margin: 0;
    }

    .actions {
      display: flex;
      gap: 10px;
    }
  }

  .content {
    .layer-list {
      .layer-item {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 8px;
        border: 1px solid #eee;
        border-radius: 4px;
        margin-bottom: 8px;
        cursor: pointer;
        transition: all 0.3s;

        &:hover {
          background-color: #f5f7fa;
        }

        &.active {
          background-color: #ecf5ff;
          border-color: #409eff;
        }

        .layer-info {
          display: flex;
          align-items: center;
          gap: 8px;

          i {
            font-size: 16px;
          }

          span {
            font-size: 14px;
          }
        }
      }
    }
  }
}
</style> 